package git.jas0nch.boottest.dao.Impl;

import git.jas0nch.boottest.dao.IEmployeeDAO;
import git.jas0nch.boottest.dao.Impl.DepartmentDao;
import git.jas0nch.boottest.pojo.Department;
import git.jas0nch.boottest.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * boottest
 *
 * @author: git.jas0nch
 * date: 2021-11-01
 */

@Repository
public class EmployeeDAOImpl implements IEmployeeDAO {

    @Autowired
    public DepartmentDao departmentDao;

    private static Map<String, Employee> sql_map_Employees = null;
    static {
        // 日期可在构造器中实现
        sql_map_Employees = new HashMap<>();
        //
        sql_map_Employees.put("1",new Employee("1","jason1","123@cn.com",1,
                new Department("1","生活部"),new Date()));
        sql_map_Employees.put("2",new Employee("2","jason2","123@cn.com",0,
                new Department("2","后勤部"),new Date()));
        sql_map_Employees.put("3",new Employee("3","jason3","123@cn.com",1,
                new Department("3","学习部"),new Date()));
        sql_map_Employees.put("4",new Employee("4","jason4","123@cn.com",0,
                new Department("1","生活部"),new Date()));
        sql_map_Employees.put("5",new Employee("5","jason5","123@cn.com",1,
                new Department("2","后勤部"),new Date()));
    }
    // get
    public Collection<Employee> getEmployees(){
        return sql_map_Employees.values();
    }
    // create
    private static int cnt_id = 5;
    public boolean createEmployee(Employee employee){
        employee.setId(cnt_id+++"");
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        if (sql_map_Employees.put(employee.getId(),employee)!=null){
            return true;
        }else{
            return false;
        }
    }
    // get by id;
    public Employee getEmployeeById(String id){
        return sql_map_Employees.get(id);
    }
    // delete
    public boolean deleteEmployee(String id){
        if(sql_map_Employees.remove(id)!=null){
            return true;
        }else {
            return false;
        }
    }
}
